package com.github.aloxc.plugin.restplus.common;



import com.github.aloxc.plugin.restplus.annotations.*;
import com.github.aloxc.plugin.restplus.method.HttpMethod;
import com.github.aloxc.plugin.DonotGuard;
import com.intellij.openapi.util.IconLoader;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

/**
 * 常量类
 *
 * @author liyh
 */
public interface Consts {
    String SLASH = "/";
    interface JAVA{
        String NOT_NULL ="javax.validation.constraints.NotNull";

        String HTTP_SERVLET_REQUEST ="javax.servlet.http.HttpServletRequest";

        String HTTP_SERVLET_RESPONSE ="javax.servlet.http.HttpServletResponse";
    }

    interface YAPI{
        /**
         * 目录
         */
        String MENU="tool-temp";

        /**
         * 获取菜单列表
         */
        String CAT_MENU ="/api/interface/getCatMenu";

        /**
         * 新增或者更新接口
         */
        String SAVE ="/api/interface/save";
        /**
         * 获取接口菜单列表
         */
        String LIST_MENU ="/api/interface/list_menu";
        /**
         * 更新接口
         */
        String UP ="/api/interface/up";
        /**
         * 获取接口列表数据
         */
        String LIST ="/api/interface/list";
        /**
         * 新增接口
         */
        String ADD ="/api/interface/add";
        /**
         * 新增接口分类
         */
        String ADD_CAT ="/api/interface/add_cat";
        /**
         * 获取接口数据
         */
        String GET ="/api/interface/get";

        /**
         * 查看接口的地址
         */
        String INFO  = "/project/%s/interface/api/%s";
        /**
         * 查看cat接口地址
         */
        String CAT_INFO  = "/project/%s/interface/api/cat_%s";

        /**
         * 读取某个cat下的接口列表
         */
        String API_LIST_IN_CAT  = "/api/interface/list_cat";
    }

    String FORMAT_REGISTER = "formatName";
    String CONFIG_PATH = "configPath";

    String SPLIT = " : ";

    String SIGN = "-";

    String C_NULL = "k";
    String C_NUM = "n";
    String C_OBJ = "o";
    String C_ARR = "a";
    String C_STR = "v";
    String C_BOOL = "b";

    String S_NULL = C_NULL + SIGN;
    String S_NUM = C_NUM + SIGN;
    String S_OBJ = C_OBJ + SIGN;
    String S_ARR = C_ARR + SIGN;
    String S_STR = C_STR + SIGN;
    String S_BOOL = C_BOOL + SIGN;

    String ARRAY = "Array";
    String OBJECT = "Object";
    
    char DOT = 36;
    class Method {
        public static Icon get(HttpMethod method,boolean foundRequestBody) {
            if (method == null) {
                return Method.UNDEFINED;
            }
            if (method.equals(HttpMethod.GET)) {
                return Method.GET;
            } else if (method.equals(HttpMethod.POST)) {
                if(foundRequestBody){
                    return Method.POSTBODY;
                }
                return Method.POST;
            } else if (method.equals(HttpMethod.PUT)) {
                return Method.PUT;
            } else if (method.equals(HttpMethod.PATCH)) {
                return Method.PATCH;
            } else if (method.equals(HttpMethod.DELETE)) {
                return Method.DELETE;
            } else if (method.equals(HttpMethod.PURGE)) {
                return Method.PURGE;
            }
            return null;
        }

        public static Icon GET = IconLoader.getIcon("/icons/method/get.png"); // 16x16 GREEN
        public static Icon PUT = IconLoader.getIcon("/icons/method/put.png"); // 16x16 ORANGE
        public static Icon POST = IconLoader.getIcon("/icons/method/post.png"); // 16x16 BLUE
        public static Icon POSTBODY = IconLoader.getIcon("/icons/method/postbody.png"); // 16x16 BLUE
        public static Icon PATCH = IconLoader.getIcon("/icons/method/patch.png"); // 16x16 GRAY
        public static Icon DELETE = IconLoader.getIcon("/icons/method/delete.png"); // 16x16 RED
        public static Icon PURGE = IconLoader.getIcon("/icons/method/purge.png"); // 16x16 RED
        public static Icon HEAD = IconLoader.getIcon("/icons/method/head.png"); // 16x16 RED
        public static Icon UNDEFINED = IconLoader.getIcon("/icons/method/undefined.png"); // 16x16 GRAY
        // OPTIONS HEAD
    }
    interface Icons{
        Icon OUT_PUT = IconLoader.getIcon("/icons/output.png"); // 16x16
        Icon DESC = IconLoader.getIcon("/icons/desc.png"); // 16x16
        Icon ASC = IconLoader.getIcon("/icons/asc.png"); // 16x16
        Icon MODULE_NODE = IconLoader.getIcon("/icons/moduleNode.png"); // 16x16
        Icon MODULE = IconLoader.getIcon("/icons/module.png"); // 16x16
        Icon GROUP = IconLoader.getIcon("/icons/group.png"); // 16x16
        Icon TREE = IconLoader.getIcon("/icons/tree.png"); // 16x16
        Icon CODE = IconLoader.getIcon("/icons/code.png"); // 16x16
        Icon METHOD = IconLoader.getIcon("/icons/method.png"); // 16x16
        Icon HISTORY = IconLoader.getIcon("/icons/history.png"); // 16x16
        Icon SERVICE = IconLoader.getIcon("/icons/restplus.png"); // 16x16
        Icon NEW = IconLoader.getIcon("/icons/new.png"); // 16x16
        Icon NOW = IconLoader.getIcon("/icons/now.png"); // 16x16
        Icon HELP = IconLoader.getIcon("/icons/help16.svg"); // 16x16
        Icon CUR = IconLoader.getIcon("/icons/cur.png"); // 16x16
        Icon URL = IconLoader.getIcon("/icons/url.png"); // 16x16
        Icon FULL_URL = IconLoader.getIcon("/icons/fullurl.png"); // 16x16
        Icon PUSH_YAPI = IconLoader.getIcon("/icons/pushyapi.png"); // 16x16
        Icon SAVE = IconLoader.getIcon("/icons/save.png"); // 16x16
        Icon ENVIROMENT = IconLoader.getIcon("/icons/environment.png"); // 16x16
        Icon ENVIROMENT16 = IconLoader.getIcon("/icons/environment16.png"); // 16x16
        Icon EXCEPTION = IconLoader.getIcon("/icons/exception.png"); // 16x16
        Icon SAVE_AS = IconLoader.getIcon("/icons/saveas.png"); // 16x16
        Icon A = IconLoader.getIcon("/icons/a.png"); // 16x16
        Icon V = IconLoader.getIcon("/icons/v.png"); // 16x16
        Icon O = IconLoader.getIcon("/icons/o.png"); // 16x16
        Icon N = IconLoader.getIcon("/icons/n.png"); // 16x16
        Icon K = IconLoader.getIcon("/icons/k.gif"); // 16x16
        Icon YES = IconLoader.getIcon("/icons/yes.png"); // 16x16
        Icon NO = IconLoader.getIcon("/icons/no.png"); // 16x16
        Icon HALF = IconLoader.getIcon("/icons/half.png"); // 16x16
        Icon RUN = IconLoader.getIcon("/icons/run.png"); // 16x16
        Icon SWAGGER = IconLoader.getIcon("/icons/swagger.png"); // 16x16
        Icon FEIGN = IconLoader.getIcon("/icons/feign.png"); // 16x16

        interface Code{
            Icon ERROR = IconLoader.getIcon("/icons/code/error.png");
            Icon C204 = IconLoader.getIcon("/icons/code/204.png");
            Icon C206 = IconLoader.getIcon("/icons/code/206.png");
            Icon C301 = IconLoader.getIcon("/icons/code/301.png");
            Icon C302 = IconLoader.getIcon("/icons/code/302.png");
            Icon C303 = IconLoader.getIcon("/icons/code/303.png");
            Icon C304 = IconLoader.getIcon("/icons/code/304.png");
            Icon C400 = IconLoader.getIcon("/icons/code/400.png");
            Icon C401 = IconLoader.getIcon("/icons/code/401.png");
            Icon C403 = IconLoader.getIcon("/icons/code/403.png");
            Icon C5404 = IconLoader.getIcon("/icons/code/404.png");
            Icon C500 = IconLoader.getIcon("/icons/code/500.png");
            Icon C503 = IconLoader.getIcon("/icons/code/503.png");
        }
    }

    interface TranslateTools{
        /**
         * 腾讯翻译
         */
        public static final String TENCENT_TRANSLATOR = "腾讯翻译";
        /**
         * 百度翻译
         */
        public static final String BAIDU_TRANSLATOR = "百度翻译";
        /**
         * 有道翻译
         */
        public static final String YOUDAO_TRANSLATOR = "有道翻译";
        /**
         * 金山翻译
         */
        public static final String JINSHAN_TRANSLATOR = "金山翻译";
        /**
         * 关闭翻译
         */
        public static final String CLOSE_TRANSLATOR = "关闭（只使用自定义翻译）";
    }

    Color MAIN_COLOR = new Color(255, 162, 45);
    Color COOKIE_BACKGROUND_COLOR = new Color(253, 245, 230);
    Color LINE_NUMBER_BACKGROUND_COLOR = new Color(255, 162, 45,10);
    String YAPI_CONFIG_KEY = "yapiConfig";
    String YAPI_CONFIG_ATTRIBUTE_KEY = "YAPI";
    /**
     * 变量类型
     */
    @DonotGuard
    enum VariateType {
        /**
         * 请求头
         */
        @DonotGuard
        HEADER("Header"),
        /**
         * 请求cookie
         */
        @DonotGuard
        COOKIE("Cookie"),

        /**
         * 查询串
         */
        @DonotGuard
        QUERY("Query"),

        /**
         * 未定义
         */
        @DonotGuard
        NONE("---");

        private VariateType(String type) {
            this.type = type;
        }

        public String getType() {
            return type;
        }

        private String type;
    }


    Dimension BUTTON_DIMENSION = new Dimension(16, 16);
    Dimension TOOLBAR_BUTTON_DIMENSION = new Dimension(24, 24);

    int PER_REQUEST_HISTORY_COUNT = 50;
    String HISTORY_REQUESTS_ROOT = "/restplus";
    String HISTORY_FILE_EXTEND_NAME = ".json";
    String PLUGIN_ID = "RestPlus";


    String SWAGGERS = "Swaggers";
    String SERVICES = "Services";

    interface Swagger{
        String API = "io.swagger.annotations.Api";
        String API_OPERATION = "io.swagger.annotations.ApiOperation";
        String API_MODEL_PROPERTY = "io.swagger.annotations.ApiModelProperty";
        String API_MODEL = "io.swagger.annotations.ApiModel";
    }

    interface SpringCloud{
        String FEIGN_ANNOTATION = "org.springframework.cloud.openfeign.FeignClient";
    }

    /**
     * 服务类型
     */
    enum ServiceType{
        /**
         * spring服务
         */
        SPRING,

        /**
         * jax-rs服务
         */
        JAXRS,
        /**
         * Micronaut服务
         */
        MICRONAUT,

        /**
         * dubbo服务，暂未支持
         */
        DUBBO,

        ;

        /**
         * 通过psiMethod读取服务类型
         * @param psiMethod
         * @return
         */
        public static ServiceType getServiceType(PsiMethod psiMethod){
            PsiAnnotation[] annotations = psiMethod.getModifierList().getAnnotations();

            for (PsiAnnotation annotation : annotations) {
                boolean match = Arrays.stream(SpringRequestMethodAnnotation.values()).map(sra -> sra.getQualifiedName()).anyMatch(name -> name.equals(annotation.getQualifiedName()));
                if(match) {
                    return SPRING;
                }
            }

            for (PsiAnnotation annotation : annotations) {
                boolean match = Arrays.stream(JaxrsHttpMethodAnnotation.values()).map(sra -> sra.getQualifiedName()).anyMatch(name -> name.equals(annotation.getQualifiedName()));
                if(match) {
                    return JAXRS;
                }
            }
            for (PsiAnnotation annotation : annotations) {
                boolean match = Arrays.stream(MicronautRequestMethodAnnotation.values()).map(sra -> sra.getQualifiedName()).anyMatch(name -> name.equals(annotation.getQualifiedName()));
                if(match) {
                    return MICRONAUT;
                }
            }
            return null;
        }

        /**
         * 通过psiClass读取服务类型
         * @param psiClass
         * @return
         */
        public static ServiceType getServiceType(PsiClass psiClass){
            PsiAnnotation[] annotations = psiClass.getModifierList().getAnnotations();

            for (PsiAnnotation annotation : annotations) {
                boolean match = Arrays.stream(SpringControllerAnnotation.values()).map(sra -> sra.getQualifiedName()).anyMatch(name -> name.equals(annotation.getQualifiedName()));
                if(match) {
                    return SPRING;
                }
            }

            for (PsiAnnotation annotation : annotations) {
                boolean match = Arrays.stream(JaxrsPathAnnotation.values()).map(sra -> sra.getQualifiedName()).anyMatch(name -> name.equals(annotation.getQualifiedName()));
                if(match) {
                    return JAXRS;
                }
            }
            for (PsiAnnotation annotation : annotations) {
                boolean match = Arrays.stream(MicronautControllerAnnotataion.values()).map(sra -> sra.getQualifiedName()).anyMatch(name -> name.equals(annotation.getQualifiedName()));
                if(match) {
                    return MICRONAUT;
                }
            }
            return null;
        }
    }
}
